package module.file

class DFileController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [DFileInstanceList: DFile.list(params), DFileInstanceTotal: DFile.count()]
    }

    def create = {
        def DFileInstance = new DFile()
        DFileInstance.properties = params
        return [DFileInstance: DFileInstance]
    }

    def save = {
        def DFileInstance = new DFile(params)

        def file = request.getFile("data")
        DFileInstance.name = file.name //.getOriginalFilename() 这里是springextras的一个错误，这两个名称，正好反了。
        DFileInstance.type = file.contentType
        log.debug file
        
        DFile.withTransaction{
            if (DFileInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.created.message', args: [message(code: 'DFile.label', default: 'DFile'), DFileInstance.id])}"
                redirect(action: "show", id: DFileInstance.id)
            }
            else {
                render(view: "create", model: [DFileInstance: DFileInstance])
            }
        }
        
    }

    def show = {
        def DFileInstance = DFile.get(params.id)
        if (!DFileInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'DFile.label', default: 'DFile'), params.id])}"
            redirect(action: "list")
        }
        else {
            [DFileInstance: DFileInstance]
        }
    }

    def delete = {
        DFile.withTransaction{
            def DFileInstance = DFile.get(params.id)
            if (DFileInstance) {
                try {
                    DFileInstance.delete(flush: true)
                    flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'DFile.label', default: 'DFile'), params.id])}"
                    redirect(action: "list")
                }
                catch (org.springframework.dao.DataIntegrityViolationException e) {
                    flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'DFile.label', default: 'DFile'), params.id])}"
                    redirect(action: "show", id: params.id)
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'DFile.label', default: 'DFile'), params.id])}"
                redirect(action: "list")
            }
        }
    }
}
